package com.aote.entity.action;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.aote.component.format.FormatFactory;
import com.aote.mvc.event.Action;
import com.aote.mvc.event.ActionFactory;
import com.aote.util.ExpressionHelper;
import com.aote.util.StringHelper;
import com.browsesoft.Tools;
import com.browsesoft.dbtools.DBTools;
import com.browsesoft.htmlcomponent.HTMLBasicComponent;
import com.browsesoft.user.User;

/**
 * 一对多动作处理
 * 
 */
public class OneToManyAction extends Action {

	/**
	 * 处理器集合
	 */
	private List processors = new LinkedList();

	public void performAction(Element config, HttpServletRequest request,
			Map attrs) {
		// 根据配置的sql找到多个属性集合
		String sql = config.getAttribute("findmanysql");
		sql = StringHelper.replaceStr(attrs, sql);
		List manyAttrs = getAttrs(sql);
		// 如果有可处理数据,产生处理器
		if (manyAttrs.size() > 0) {
			this.processors = createProcessors(config);
		}
		// 进行一对多处理
		Iterator iter = manyAttrs.iterator();
		while (iter.hasNext()) {
			Map oneOfMany = (Map) iter.next();
			// 循环调用处理器处理
			callProcess(attrs, oneOfMany, request);
		}
	}

	/**
	 * 处理器循环处理
	 */
	public void callProcess(Map one, Map oneOfMany, HttpServletRequest request) {
		Iterator iter = this.processors.iterator();
		while (iter.hasNext()) {
			Processor p = (Processor) iter.next();
			if (p.canDead(one, oneOfMany)) {
				p.process(one, oneOfMany, request);
			}
		}
	}

	/**
	 * 创建处理器
	 */
	private List createProcessors(Element config) {
		List result = new LinkedList();
		NodeList list = config.getElementsByTagName("process");
		for (int i = 0; i < list.getLength(); i++) {
			Element elem = (Element) list.item(i);
			Processor p = new Processor(elem, this.component);
			result.add(p);
		}
		return result;
	}

	/**
	 * 得到多属性集合
	 */
	private List getAttrs(String sql) {
		try {
			String[][] datas = DBTools.executeQueryWithTableHead(sql);
			return DBTools.arrayToHashtable(datas);
		} catch (Exception e) {
			throw new RuntimeException("sql错误:" + sql, e);
		}
	}
}

/**
 * 处理器
 */
class Processor {
	Element config = null;

	HTMLBasicComponent component = null;

	ActionFactory actionFactory = null;

	public Processor(Element config, HTMLBasicComponent component) {
		this.config = config;
		this.component = component;
		this.actionFactory = new ActionFactory(component);
	}

	/**
	 * 判断是否可处理
	 */
	public boolean canDead(Map one, Map oneOfMany) {
		throw new UnsupportedOperationException();
	}

	/**
	 * 处理
	 */
	public void process(Map one, Map oneOfMany, HttpServletRequest request) {
		Map tempAttrs = getMapAttrs(this.config, one, oneOfMany, request);
		NodeList list = this.config.getElementsByTagName("executeaction");
		for (int i = 0; i < list.getLength(); i++) {
			Element elem = (Element) list.item(i);
			String actionName = elem.getAttribute("name");
			String sendAttr = elem.getAttribute("sendattr");	
			if (sendAttr.equals("one")) {
				Map temp = new HashMap();
				temp.putAll(one);
				temp.putAll(tempAttrs);
				this.actionFactory.doAction(actionName, request, temp);
			} else if (sendAttr.equals("many")) {
				Map temp = new HashMap();
				temp.putAll(oneOfMany);
				temp.putAll(tempAttrs);
				this.actionFactory.doAction(actionName, request, temp);
			}
		}
	}

	/**
	 * 产生临时属性
	 */
	private Map getMapAttrs(Element config, Map one, Map many,
			HttpServletRequest request) {
		throw new UnsupportedOperationException();
	}
}
